package com.module.antiFake.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;

import com.module.antiFake.util.AntiFakeBeanUtil;
import com.module.antiFake.vo.AntiFakeComplain;
import com.module.antiFake.vo.AntiFakeFile;
import com.module.antiFake.vo.AntiFakeWarn;



public class AntiFakeComplainDao extends GenericHibernateDao<AntiFakeComplain,Integer>{
	private AntiFakeWarnDao warnDao = AntiFakeWarnDao.getInstance();
	private AntiFakeFileDao fileDao = AntiFakeFileDao.getInstance();
	private AntiFakeComplainDao(){
		
	}
	private static AntiFakeComplainDao dao;
	public synchronized static AntiFakeComplainDao getInstance(){
		if(dao == null){
			dao = new AntiFakeComplainDao();
		}
		return dao;
	}
	
	@SuppressWarnings("unchecked")
	public Page  getList(Session session,AntiFakeComplain obj,Integer start, Integer limit) throws Exception{
		Page page = new Page();
		String hql1 =" from AntiFakeComplain as obj where 1=1";
		String hql2 =" select count(*) from AntiFakeComplain as obj where 1=1";
		if(StringUtils.isNotBlank(obj.getState())){
			hql1=hql1 +  " and obj.state = '"+obj.getState()+"'";
			hql2=hql2 +  " and obj.state = '"+obj.getState()+"'";
		}
		if(StringUtils.isNotBlank(obj.getProductId())){
			hql1=hql1 +  " and obj.productId = '"+obj.getProductId()+"'";
			hql2=hql2 +  " and obj.productId = '"+obj.getProductId()+"'";
		}
		if(StringUtils.isNotBlank(obj.getOrgId())){
			hql1=hql1+" and obj.orgId='"+obj.getOrgId()+"'";
			hql2=hql2+" and obj.orgId='"+obj.getOrgId()+"'";
		}
		hql1=hql1+" order by id desc ";
		Query query = session.createQuery(hql1);
		query.setFirstResult(start).setMaxResults(limit);
		page.setTotal((long) ((Long) session.createQuery(hql2).uniqueResult()).intValue());
		page.setRows(query.list());		
		return page;
	}
	/**
	 * 根据手机串号获得投诉记录
	 * @param bean
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List listAntiFakeComplainByImei(Session session ,AntiFakeBeanUtil bean){
		String imei = bean.getAntiFakeComplain().getImei();
		String hql = " from AntiFakeComplain as a where a.imei='"+imei+"'" + " order by complainDate desc";
		Query query=session.createQuery(hql);
		return query.list();
	}

	public void saveComplain(Session session,AntiFakeComplain antiFakeComplain,List<AntiFakeFile> listfile) {
        //保存数据到投诉表中
        save(session,antiFakeComplain);
        if(listfile != null){
        	//保存附件数据
            for(int i=0;i<listfile.size();i++){
            	AntiFakeFile file=listfile.get(i);
            	file.setComplainId(antiFakeComplain.getId());
            	fileDao.save(session, file);
            }
        }    
        AntiFakeWarn antiFakeWarn = AntiFakeWarn.copyFromAntiFakeComplain(antiFakeComplain);
        warnDao.save(session,antiFakeWarn);
        antiFakeComplain.setWarnId(antiFakeWarn.getId().intValue());
	}
	
	public Integer saveAntiFakeComplain(Session session,AntiFakeComplain bean){
		 int n=(Integer)session.save(bean);
		 return n;
	}
	/**
	 * 按ID倒序获取AntiFakeComplain中待处理的的一条数据
	 * @param session
	 * @param orgId
	 * @return
	 */
	public Integer getAntiFakeComplainByOrgId(Session session,Integer pkid,String orgId){
		Integer id = null;
		
		List<AntiFakeComplain> list = this.findBySQL(session,"from AntiFakeComplain where orgId='"+orgId+"' and id<"+pkid+" and state ='000000' ORDER BY id desc");
		if(!list.isEmpty() && list.size()>0){
			id = list.get(0).getId();
		}
		return id;
	}
}
